 

package com.zdkj.subway.rca.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.constant.CommonConstants;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.subway.rca.entity.SwCkOthenXaztTicketRevenueManagement;
import com.zdkj.subway.rca.entity.SwCkOthenXaztTicketRevenueManagementLog;
import com.zdkj.subway.rca.service.SwCkOthenXaztTicketRevenueManagementLogService;
import com.zdkj.subway.rca.service.SwCkOthenXaztTicketRevenueManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.format.DateTimeFormatter;


/**
 * 报表调账-其他运营商西安中铁票务收入管理日志表
 *
 * @author stw
 * @date 2020-10-31 08:14:50
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swckothenxaztticketrevenuemanagementlog" )
@Api(value = "swckothenxaztticketrevenuemanagementlog", tags = "报表调账-其他运营商票务收入管理日志表管理")
public class SwCkOthenXaztTicketRevenueManagementLogController {

    private final SwCkOthenXaztTicketRevenueManagementLogService swCkOthenXaztTicketRevenueManagementLogService;

    @Autowired
    private final SwCkOthenXaztTicketRevenueManagementService swCkOthenXaztTicketRevenueManagementService;
    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
	public R getSwCkOthenXaztTicketRevenueManagementLogPage(Page page, String importStartDate, String importEndDate, String beginTime, String endTime) {
        SwCkOthenXaztTicketRevenueManagementLog swCkOthenXaztTicketRevenueManagementLog = new SwCkOthenXaztTicketRevenueManagementLog();
        if(StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime)){
            return R.ok(swCkOthenXaztTicketRevenueManagementLogService.
                    page(page, Wrappers.query(swCkOthenXaztTicketRevenueManagementLog)
                            .select("id, uuid, service_id, begin_time, end_time, submit_person_id, submit_person_code, submit_person_name, dept_id, creator_id, creator_name, MAX(create_time) as createTime, update_time, del_flag")
                            .or(Wrapper -> Wrapper.le("begin_time",beginTime).between("end_time",beginTime,endTime).eq("del_flag","0"))
                            .or(Wrapper -> Wrapper.le("end_time",endTime).between("begin_time",beginTime,endTime).eq("del_flag","0"))
                            .or(Wrapper -> Wrapper.gt("end_time",endTime).between("begin_time",beginTime,endTime).eq("del_flag","0"))
                            .groupBy("begin_time","end_time")
                            .orderByDesc("create_time")
                    ));
        }else {
            return R.ok(swCkOthenXaztTicketRevenueManagementLogService.
                    page(page, Wrappers.query(swCkOthenXaztTicketRevenueManagementLog)
                            .select("id, uuid, service_id, begin_time, end_time, submit_person_id, submit_person_code, submit_person_name, dept_id, creator_id, creator_name, MAX(create_time) as createTime, update_time, del_flag")
                            .eq(StringUtils.isNotEmpty(beginTime),"begin_time",beginTime)
                            .eq(StringUtils.isNotEmpty(endTime),"end_time",endTime)
                            .eq("del_flag","0")
                            .groupBy("begin_time","end_time")
                            .orderByDesc("create_time")
                    ));
        }


    }

    /**
     * 导出模板
     * @param
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "导出模板", notes = "导出模板")
    @GetMapping("/exportTamplate")
    @PreAuthorize("@pms.hasPermission('sw_ck_othen_xazt_ticket_revenue_management_import')")
    public void exportTamplate(HttpServletResponse response) {
        swCkOthenXaztTicketRevenueManagementLogService.exportTamplates(response);
    }
    /**
     * 导入
     * @param file
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导入", notes = "导入")
    @SysLog("导入其他运营商西安中铁票务收入")
    @PostMapping(value = "/saveBatch", headers = "content-type=multipart/form-data")
    @PreAuthorize("@pms.hasPermission('sw_ck_othen_xazt_ticket_revenue_management_import')")
    public R saveBatch(@RequestParam(value = "file") MultipartFile file) throws Exception {
        return R.ok((Object) this.swCkOthenXaztTicketRevenueManagementLogService.saveBatchWithExcel(file));
    }

    /**
     * 浏览
     * @return
     */
    @ApiOperation(value = "浏览，（id:浏览的那条数据的ID）", notes = "浏览，（id：浏览的那条数据的ID）")
    @GetMapping("/listPreview" )
    @PreAuthorize("@pms.hasPermission('sw_ck_othen_xazt_ticket_revenue_management_import')")
    public R listPreview(Long id) throws Exception {
        if(id == null){
            return R.ok(CommonConstants.FAIL);
        }

        SwCkOthenXaztTicketRevenueManagementLog logObj =  this.swCkOthenXaztTicketRevenueManagementLogService.getById(id);
        SwCkOthenXaztTicketRevenueManagement swCkOthenXaztTicketRevenueManagement = new SwCkOthenXaztTicketRevenueManagement();
        //swCkOthenXaztTicketRevenueManagement.setLogId(logObj.getUuid());
		String startDate =  logObj.getBeginTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));//LocalDate.parse(logObj.getBeginTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		
		String endDate =    logObj.getEndTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));//LocalDate.parse(logObj.getEndTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));

		//boolean isDateCondition = StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(startDate);
/*

		 return R.ok(swCkOthenXaztTicketRevenueManagementService.list(Wrappers.query(swCkOthenXaztTicketRevenueManagement)
			            .lambda().between(isDateCondition, SwCkOthenXaztTicketRevenueManagement::getStatisticsDate, startDate, endDate)
                        .between(isDateCondition, SwCkOthenXaztTicketRevenueManagement::getStatisticsDate, startDate, endDate)
                        .orderBy(SwCkOthenXaztTicketRevenueManagement::getStatisticsDate))
		
		);

*/
        QueryWrapper<SwCkOthenXaztTicketRevenueManagement> queryWrapper = new QueryWrapper<SwCkOthenXaztTicketRevenueManagement>();
        queryWrapper.between("statistics_Date", startDate, endDate);

        return R.ok(swCkOthenXaztTicketRevenueManagementService.list(queryWrapper));

    }


    /**
     * 导出
     * @param
     * @return
     */
    @ApiOperation(value = "导出", notes = "导出")
    @GetMapping({"/export"})
    @SysLog("其他运营商西安中铁票务收入-导出")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "startDate", value = "开始日期：yyyy-MM-dd", dataTypeClass = String.class,required = true),
                    @ApiImplicitParam(name = "endDate", value = "结束日期：yyyy-MM-dd", dataTypeClass = String.class,required = true)
            }
    )
    public void export(String startDate,String endDate, HttpServletResponse response) {
        if(StringUtils.isEmpty(startDate) ||  StringUtils.isEmpty(endDate)){
            return;
        }
        swCkOthenXaztTicketRevenueManagementLogService.export(startDate,endDate,response);
    }
}
